<?php
/**
 * 生成订房订单操作
 * 1.获取用户信息 判断用户信息
 * 2.判断传入参数
 * 3.调用订房判断
 * 2.生成订房订单
 */

namespace app\api\service\hotel;


use app\api\model\HotelRoom;
use app\api\model\HotelRoomOrder;
use app\api\model\System;
use app\api\model\User;
use app\api\service\Token;
use app\api\validate\hotel\CheckHotelRoomOrder;
use app\lib\exception\ParameterException;
use think\Db;
use think\Exception;

class HotelRoomOrderSer extends Token
{
    // 用户信息
    private $user;

    // 获取信息
    private $postArr;

    // 房间详情
    private $hotelRoom;

    function __construct($postArr)
    {
        $this->user = User::get(self::getCurrentUid());
        $this->postArr = $postArr;
    }

    // 入口方法
    public function entrance()
    {
        (new CheckHotelRoomOrder())->goCheck();
        return $this->judgeUser();
    }

    // 判断用户信息
    private function judgeUser()
    {
        self::judgeMemberOne();
        $judgeRoom = new JudgeRoom($this->postArr['hotel_room_id'],$this->postArr['start_date'],$this->postArr['end_date']);
        $judgeRoom->entrance();
        return $this->judgeData();
    }

    // 判断剩余的数据
    private function judgeData()
    {
        $this->hotelRoom = HotelRoom::where('id',$this->postArr['hotel_room_id'])->with('imgs')->find();
//        if($this->postArr['type'] == 2) {
//            if($this->postArr['hour'] < $this->hotelRoom['estimate_min_hour'] || $this->postArr['hour'] > $this->hotelRoom['estimate_max_hour']) {
//                throw new ParameterException([
//                    'msg' => [
//                        'hotel_order' => '终点房入住时间选择错误'
//                    ]
//                ]);
//            }
//        }
        return $this->createData();
    }

    // 添加订房订单
    private function createData()
    {
        // 删除未支付的订单
        HotelRoomOrder::destroy(['user_id' => $this->user['id'],'status' => 1]);
//        $system = System::get(1);
//        if($this->postArr['type'] == 1) {
            $datetime_start = strtotime($this->postArr['start_date']);
            $datetime_end = strtotime($this->postArr['end_date']);
            $days = round(($datetime_end - $datetime_start) / 3600 / 24);
            $price = round($this->hotelRoom['price'] * $days,2);
            $member_type = 1;
            $this->postArr['hour'] = '';
//        } else {
//            $days = 1;
//            $price = $this->hotelRoom['estimate_price'];
//        }
        // 创建订单
        Db::startTrans();
        try{
            $mode = HotelRoomOrder::create([
                'user_id' => $this->user['id'],
                'price' => $price,
                'start_date' => $this->postArr['start_date'],
                'end_date' => $this->postArr['end_date'],
                'days' => $days,
                'hotel_room_id' => $this->hotelRoom['id'],
                'hotel_room_name' => $this->hotelRoom['name'],
                'hotel_room_img_id' => $this->hotelRoom['imgs'][0]['img_id'],
                'order_no' => self::makeOrderNo(),
                'name' => $this->postArr['name'],
                'id_no' => $this->postArr['id_no'],
                'phone' => $this->postArr['phone'],
                'type' => $this->postArr['type'],
                'estimate_time' => $this->postArr['estimate_time'],
                'hour' => $this->postArr['hour'],
                'admin_id' => $this->hotelRoom['admin_id'],
                'lin_user_id' => $this->hotelRoom['lin_user_id'],
                'member_type' => $member_type,
            ]);
            $out_trade_no = '2you2020'.$mode->id;
            $out_refund_no = '1refund2020'.$mode->id;
            $mode->out_trade_no = $out_trade_no;
            $mode->out_refund_no = $out_refund_no;
            $mode->save();
            Db::commit();
            return writeJson(201, [ 'out_trade_no'=>$out_trade_no ], '订单生成成功！');
        }
        catch (Exception $ex) {
            Db::rollback();
            throw $ex;
        }
    }
}